#include "dominion_helpers.h"
#include "dominion.h"
#include "rngs.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int randomGameState(int players, struct gameState *g)
{
	int i, j;
	g->numPlayers = players;
	
	for (i = 0; i < 27; i++) {
		g->supplyCount[i] = floor(Random() * 30);
		g->embargoTokens[i] = floor(Random() * 30);
	}
	g->outpostPlayed = floor(Random() * 10);
	g->whoseTurn = floor(Random() * players);
	g->outpostTurn = g->whoseTurn;
	g->phase = 0;
	g->numActions = floor(Random() * 10);
	g->coins = floor(Random() * 10);
	g->numBuys = floor(Random() * 10);

	g->playedCardCount = floor(Random() * 10);
	for (i = 0; i < g->playedCardCount; i++) {
		g->playedCards[i] = floor(Random() * 26);
	}

	for (i = 0; i < players; i++) {
		g->handCount[i] = floor(Random() * MAX_HAND);
		g->deckCount[i] = floor(Random() * MAX_DECK);
		g->discardCount[i] = floor(Random() * MAX_DECK);

		for (j = 0; j < g->handCount[i]; j++) {
			g->hand[i][j] = floor(Random() * 26);
		}
		for (j = 0; j < g->deckCount[i]; j++) {
			g->deck[i][j] = floor(Random() * 26);
		}
		for (j = 0; j < g->discardCount[i]; j++) {
			g->discard[i][j] = floor(Random() * 26);
		}
	}
	return 0;
}

int main()
{
	printf("Random tests for adventurer card\n");
	SelectStream(2);
	PutSeed(3);

	int i, j, treasureCount, p = 1;
	struct gameState original, test;

	for (i = 0; i < 1000; i++) {
		treasureCount = 0;
		randomGameState(p, &original);

		memcpy(&test, &original, sizeof(struct gameState));
		if (memcmp(&test, &original, sizeof(struct gameState)) != 0)
			printf("error with memory copy\n");

		// manually simulate adventurer card. effect: reveal card from deck 
		// until you reaveal 2 treasure cards. put those in hand discard others.
			for (j = 0; j < original.deckCount[0]; j++) {
				if (original.deck[0][j] == copper || original.deck[0][j] == silver ||
						original.deck[0][j] == gold) {
						treasureCount++;
				}
			}
			for (j = 0; j < original.discardCount[0]; j++) {
				if (original.discard[0][j] == copper || original.discard[0][j] ==
						silver || original.discard[0][j] == gold) {
						treasureCount++;
				}
			}
			if (treasureCount == 1)
				original.handCount[0] += 1;
			if (treasureCount > 1)
				original.handCount[0] += 2;
			
			cardEffect(adventurer, 0, 0, 0, &test, 0, 0);

			if (original.handCount[0] != test.handCount[0]) {
				printf("error with hand count\n");
				printf("   treasure Count: %d\n", treasureCount);
				printf("   original hCount: %d\n", original.handCount[0]);
				printf("   test hCount: %d\n", test.handCount[0]);
			}
	}

	printf("done with tests\n");

	return 0;
}
